const Router = require('koa-router')
const { auth, checkOwn} = require('../middle/auth')
const router = new Router({
  prefix: '/users'
})

const {findAllUser,findById,addUser,updateUser,deleteUser,login, listFollowing, followingUser, unfollowing, listFollowers, checkUserExist, listQuestion, listApprovedAnswer, approvedAnswer, unapprovedAnswer, opposeAnswer, unOpposeAnswer,getCollectionsList, addCollections, removeCollections } = require('../controllers/user')
// 检查回答是否存在
const { checkAnswerExist } = require('../controllers/answer')


// 获取所有的用户
router.get('/',findAllUser)

// 获取特定的用户
router.get('/:id',findById)

// 新增用户
router.post('/',addUser)

// 更新用户
router.patch('/:id', auth, checkOwn, updateUser)
// 删除用户
router.delete('/:id',auth, checkOwn, deleteUser)
// 关注列表
router.get('/:id/following', listFollowing)
// 关注某一用户
router.put('/following/:id', auth, checkUserExist, followingUser)

// 取消关注
router.delete('/unfollowing/:id', auth, checkUserExist, unfollowing)

// 获取所有的粉丝列表
router.get('/:id/listFollowers', listFollowers)

// 获取用户的提问列表
router.get('/:id/questions', listQuestion)

// 获取用户赞同的回答列表
router.get('/:id/approvedAnswerList', listApprovedAnswer)

// 赞同答案(取消反对)
router.put('/approvedAnswer/:answerId', auth ,checkAnswerExist, approvedAnswer, unOpposeAnswer)

// 取消赞同回答
router.delete('/approvedAnswer/:answerId', auth ,checkAnswerExist, unapprovedAnswer)

// 反对答案（取消赞同）
router.put('/opposeAnswer/:answerId', auth ,checkAnswerExist, opposeAnswer, unapprovedAnswer)

// 取消反对回答
router.delete('/opposeAnswer/:answerId', auth ,checkAnswerExist, unOpposeAnswer)

// 登录用户收藏回答的列表
router.get('/collections/:id', getCollectionsList)

// 收藏回答
router.put('/collections/:answerId',auth, checkAnswerExist, addCollections)

// 取消收藏
router.delete('/collections/:answerId', auth, checkAnswerExist, removeCollections)

// 登陆
router.post('/login',login)


module.exports = router